package com.nstar.jianli.controller
{
	import com.nstar.base.weibo.WeiboControllerTemplate;
	import com.nstar.jianli.components.Toast;
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.data.MicroBlogUser;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;

	public class WeiboController extends WeiboControllerTemplate
	{
		private static var inst:WeiboController;
		
		public var logined:Boolean = false;
		
		private var _user:MicroBlogUser;//当前登录用户
		private var advicing:Boolean = false;//提意见状态
		
		
		public function WeiboController()
		{
			super();
		}
		
		override protected function addEventListeners ():void{
			getMicroBlog().addEventListener(MicroBlogEvent.LOGIN_RESULT, microBlogHandler);
			getMicroBlog().addEventListener(MicroBlogEvent.LOGOUT_RESULT,microBlogHandler);
			getMicroBlog().addEventListener(MicroBlogEvent.VERIFY_CREDENTIALS_RESULT,microBlogHandler);
			getMicroBlog().addEventListener(MicroBlogEvent.UPDATE_STATUS_RESULT,microBlogHandler);
			getMicroBlog().addEventListener(MicroBlogErrorEvent.UPDATE_STATUS_ERROR,microBlogErrorHandler);
			getMicroBlog().addEventListener(MicroBlogEvent.LOAD_STATUS_COUNTS_RESULT,microBlogHandler);
			getMicroBlog().addEventListener(MicroBlogErrorEvent.LOAD_STATUS_COUNTS_ERROR,microBlogErrorHandler);
		}		
		
		public static function getInstance():WeiboController{
			if(!inst){
				inst = new WeiboController();
			}
			return inst;
		} 
		
		public static function getMicroBlog():MicroBlog{
			return inst.getMicroBlog();
		}
		
		//提意见
		public function advice(yourAdvice:String):void{
			advicing = true;
			getMicroBlog().updateStatus(yourAdvice);
		}
		
		//微博事件处理
		private function microBlogHandler(event:MicroBlogEvent):void{
			switch(event.type){
				case MicroBlogEvent.LOGIN_RESULT:
					trace("login ok");
					logined = true;
					microBlog.verifyCredentials();//验证用户
					break;
				case MicroBlogEvent.VERIFY_CREDENTIALS_RESULT:
					_user = event.result as MicroBlogUser;
					trace("verify credentials ok, user -> "+_user.screenName+"("+_user.id+")");
					break;
				case MicroBlogEvent.UPDATE_STATUS_RESULT:
					if(advicing){
						advicing = false;
						Toast.show("谢谢您提的意见和建议，我们会尽快处理的~ :-)");
					}
					break;
				case MicroBlogEvent.LOAD_STATUS_COUNTS_RESULT://获取微博的转发和评论数
					break;
			}
			
			this.dispatchEvent(event);
		}
		
		private function microBlogErrorHandler(event:MicroBlogErrorEvent):void{
			Toast.show("处理微博消息失败,ERROR:"+event.message);
		}
		
		public function get user():MicroBlogUser{
			return this._user;
		}
	}
}